import { request } from '../../request/index'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        store: '',
        phone: false,
        userInfo: '',
        _user: '',
        active: '',
        authorizeStatus: false
    },

    // 用户授权
    handle_active(e) {
        wx.getUserProfile({
            desc: '用于完善会员资料',
            success: (res) => {
                this.setData({
                    phone: true,
                    userInfo: res,
                    active: e.target.dataset.params,
                    authorizeStatus: true
                })
                wx.setStorageSync('userInfo', res);
                wx.setStorageSync('authorizeStatus', true);
                wx.setStorageSync('active', e.target.dataset.params);
            }
          })
    },

    // 关闭手机授权弹框
    handleClose() {
        this.setData({
            phone: false
        })
    },

    // 用户授权之后打开授权手机号码弹框
    handle_phone(e) {
        let _user = this.data._user
        if (_user) {
            wx.navigateTo({
                url: `/pages/activityDetail/activityDetail`,
                success: res => {
                    res.eventChannel.emit('sendData', { data: e.target.dataset.params })
                }
            });
        } else {
            this.setData({
                phone: true,
                active: e.target.dataset.params
            })
        }
    },

    // 获取了手机号码之后跳转详情
    toActiveDetail() {
        let _active = this.data.active
        wx.navigateTo({
            url: `/pages/activityDetail/activityDetail`,
            success: res => {
                res.eventChannel.emit('sendData', { data: _active })
            }
        });
    },

    // 获取手机号码
    getPhone(e) {
        let _active = this.data.active
        if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
            this.setData({
                phone: false
            })
        } else {
            let encryptedData = e.detail.encryptedData, iv = e.detail.iv;
            wx.login({
                success: (result) => {
                    request({
                        url: '/xcxApi/login/login',
                        method: 'get',
                        data: {
                            encryptedData, 
                            iv,
                            code: result.code,
                            rawData: this.data.userInfo.rawData
                        }
                    }).then(res => {
                        wx.setStorageSync('_user', res.user);
                        wx.setStorageSync('token', res.xcxLoginToken);
                        this.setData({
                            _user: res.user,
                            phone: false
                        })
                        wx.navigateTo({
                            url: `/pages/activityDetail/activityDetail`,
                            success: res => {
                                res.eventChannel.emit('sendData', { data: _active })
                            }
                        });
                    })
                },
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        let store = wx.getStorageSync('store');
            let userInfo = wx.getStorageSync('userInfo');
            let _user = wx.getStorageSync('_user');
            let authorizeStatus = wx.getStorageSync('authorizeStatus');
            let active = wx.getStorageSync('active');
            this.setData({ userInfo, _user, authorizeStatus, active })
            
            if (store) {
                this.setData({
                    store
                })
                if (store.enterpriseStore && store.enterpriseStore.storeAbbreviation) {
                    wx.setNavigationBarTitle({
                        title: store.enterpriseStore.storeAbbreviation
                    });
                }
            } else {
                let enterpriseStoreId = null;
                if (options && options.enterpriseStoreId) {
                    enterpriseStoreId = options.enterpriseStoreId
                }
                let data;
                if (enterpriseStoreId) {
                    data = await request(
                        {
                            url: '/xcxApi/enterpriseStoreActivity/selectByEnterpriseStoreId',
                            data: {
                                enterpriseStoreId
                            },
                            method: 'get'
                        }
                    )
                } else {
                    data = await request(
                        {
                            url: '/xcxApi/enterpriseStoreActivity/selectByEnterpriseStoreId',
                            data: {},
                            method: 'get'
                        }
                    )
                }
                if (data) {
                    wx.setStorageSync('store', data);
                    this.setData({
                        store: data
                    })
                    if (data.enterpriseStore && data.enterpriseStore.storeAbbreviation) {
                        wx.setNavigationBarTitle({
                            title: data.enterpriseStore.storeAbbreviation
                        });
                    }
                }
            }
            return
        if (!options.scene) {
            wx.reLaunch({ url: `/pages/annual/annual?userId=` + 114430 });
        } else {
            let store = wx.getStorageSync('store');
            let userInfo = wx.getStorageSync('userInfo');
            let _user = wx.getStorageSync('_user');
            let authorizeStatus = wx.getStorageSync('authorizeStatus');
            let active = wx.getStorageSync('active');
            this.setData({ userInfo, _user, authorizeStatus, active })
            
            if (store) {
                this.setData({
                    store
                })
                if (store.enterpriseStore && store.enterpriseStore.storeAbbreviation) {
                    wx.setNavigationBarTitle({
                        title: store.enterpriseStore.storeAbbreviation
                    });
                }
            } else {
                let enterpriseStoreId = null;
                if (options && options.enterpriseStoreId) {
                    enterpriseStoreId = options.enterpriseStoreId
                }
                let data;
                if (enterpriseStoreId) {
                    data = await request(
                        {
                            url: '/xcxApi/enterpriseStoreActivity/selectByEnterpriseStoreId',
                            data: {
                                enterpriseStoreId
                            },
                            method: 'get'
                        }
                    )
                } else {
                    data = await request(
                        {
                            url: '/xcxApi/enterpriseStoreActivity/selectByEnterpriseStoreId',
                            data: {},
                            method: 'get'
                        }
                    )
                }
                if (data) {
                    wx.setStorageSync('store', data);
                    this.setData({
                        store: data
                    })
                    if (data.enterpriseStore && data.enterpriseStore.storeAbbreviation) {
                        wx.setNavigationBarTitle({
                            title: data.enterpriseStore.storeAbbreviation
                        });
                    }
                }
            }
        }
        
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})